﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_KeywordController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回关键词记录列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            qp.Orderfld = " keyword desc ";
            IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
            List<Normal_KeywordEntity> normal_keywordList = Normal_KeywordSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_keywordList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看关键词记录 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
            int intId=Utils.StringToInt(actionId,0);
            Normal_KeywordEntity normal_keyword = Normal_KeywordSv.Look(intId);
            if (normal_keyword != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_keyword), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新关键词记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Normal_KeywordEntity normal_keyword = new Normal_KeywordEntity();
                normal_keyword.DataTable_Action = DataTableAction.Update;
                normal_keyword.Id = intId;
                normal_keyword.Keyword = (String)Utils.GetQueryAndFormString("Keyword", this, 40,  1 , DataType.Str);
                normal_keyword.KeyWordUrl = (String)Utils.GetQueryAndFormString("KeyWordUrl", this, 100, 1, DataType.Str);
                normal_keyword.SearchNum = (Int32)Utils.GetQueryAndFormString("SearchNum", this, 10,  1 , DataType.Int);
                normal_keyword.IsHot = (Int32)Utils.GetQueryAndFormString("IsHot", this, 6, 1, DataType.Int);
                IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
                if (Normal_KeywordSv.Update(normal_keyword) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增关键词记录
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Normal_KeywordEntity normal_keyword = new Normal_KeywordEntity();
            normal_keyword.DataTable_Action = DataTableAction.Insert;
            normal_keyword.Keyword = (String)Utils.GetQueryAndFormString("Keyword", this, 40,  1 , DataType.Str);
            normal_keyword.SearchNum = (Int32)Utils.GetQueryAndFormString("SearchNum", this, 10,  1 , DataType.Int);
            normal_keyword.IsHot = (Int32)Utils.GetQueryAndFormString("IsHot", this, 6, 1, DataType.Int);
            normal_keyword.KeyWordUrl = (String)Utils.GetQueryAndFormString("KeyWordUrl", this, 100, 1, DataType.Str);
           IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
           if (Normal_KeywordSv.Insert(normal_keyword) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除关键词记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Normal_KeywordEntity normal_keyword = new Normal_KeywordEntity();
                normal_keyword.Id = intId;
                IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
                if (Normal_KeywordSv.Delete(normal_keyword) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除关键词记录
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Normal_KeywordEntity> Normal_KeywordSv = ServiceBuilder.BuildNormal_KeywordService();
                if (Normal_KeywordSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
